// Copyright 2011 David Galles, University of San Francisco. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of the University of San Francisco
import { Algorithm, addControlToAlgorithmBar } from "@/components/algorithm/Algorithm";

var ARRAY_START_X = 100;
var ARRAY_START_Y = 200;
var ARRAY_ELEM_WIDTH = 50;
var ARRAY_ELEM_HEIGHT = 50;

var ARRRAY_ELEMS_PER_LINE = 15;
var ARRAY_LINE_SPACING = 130;

var HEAD_POS_X = 180;
var HEAD_POS_Y = 100;
var HEAD_LABEL_X = 130;
var HEAD_LABEL_Y = 100;

var TAIL_POS_X = 280;
var TAIL_POS_Y = 100;
var TAIL_LABEL_X = 230;
var TAIL_LABEL_Y = 100;

var QUEUE_LABEL_X = 50;
var QUEUE_LABEL_Y = 30;
var QUEUE_ELEMENT_X = 120;
var QUEUE_ELEMENT_Y = 30;

var INDEX_COLOR = "#0000FF"

var SIZE = 15;

export class QueueArray extends Algorithm {
    constructor(am, w, h) {
        super();
        this.init(am, w, h);

    }
    init(am, w, h) {
        super.init.call(this, am, w, h);
        this.addControls();
        this.nextIndex = 0;
        this.commands = [];
        //this.tail_pos_y = h - LINKED_LIST_ELEM_HEIGHT;
        //	this.tail_label_y = this.tail_pos_y;
        this.setup();
        this.initialIndex = this.nextIndex;
    }
    addControls() {
        this.controls = [];
        this.enqueueField = addControlToAlgorithmBar("Text", "");
        this.enqueueField.onkeydown = this.returnSubmit(this.enqueueField, this.enqueueCallback.bind(this), 6);
        this.enqueueButton = addControlToAlgorithmBar("Button", "入队");
        this.enqueueButton.onclick = this.enqueueCallback.bind(this);
        this.controls.push(this.enqueueField);
        this.controls.push(this.enqueueButton);

        this.dequeueButton = addControlToAlgorithmBar("Button", "出队");
        this.dequeueButton.onclick = this.dequeueCallback.bind(this);
        this.controls.push(this.dequeueButton);

        this.clearButton = addControlToAlgorithmBar("Button", "清空队列");
        this.clearButton.onclick = this.clearCallback.bind(this);
        this.controls.push(this.clearButton);

    }
    enableUI(event) {
        for (var i = 0; i < this.controls.length; i++) {
            this.controls[i].disabled = false;
        }


    }
    disableUI(event) {
        for (var i = 0; i < this.controls.length; i++) {
            this.controls[i].disabled = true;
        }
    }
    setup() {

        this.nextIndex = 0;

        this.arrayID = new Array(SIZE);
        this.arrayLabelID = new Array(SIZE);
        for (var i = 0; i < SIZE; i++) {

            this.arrayID[i] = this.nextIndex++;
            this.arrayLabelID[i] = this.nextIndex++;
        }
        this.headID = this.nextIndex++;
        var headLabelID = this.nextIndex++;
        this.tailID = this.nextIndex++;
        var tailLabelID = this.nextIndex++;

        this.arrayData = new Array(SIZE);
        this.head = 0;
        this.tail = 0;
        this.leftoverLabelID = this.nextIndex++;


        for (var i = 0; i < SIZE; i++) {
            var xpos = (i % ARRRAY_ELEMS_PER_LINE) * ARRAY_ELEM_WIDTH + ARRAY_START_X;
            var ypos = Math.floor(i / ARRRAY_ELEMS_PER_LINE) * ARRAY_LINE_SPACING + ARRAY_START_Y;
            this.cmd("CreateRectangle", this.arrayID[i], "", ARRAY_ELEM_WIDTH, ARRAY_ELEM_HEIGHT, xpos, ypos);
            this.cmd("CreateLabel", this.arrayLabelID[i], i, xpos, ypos + ARRAY_ELEM_HEIGHT);
            this.cmd("SetForegroundColor", this.arrayLabelID[i], INDEX_COLOR);

        }
        this.cmd("CreateLabel", headLabelID, "头", HEAD_LABEL_X, HEAD_LABEL_Y);
        this.cmd("CreateRectangle", this.headID, 0, ARRAY_ELEM_WIDTH, ARRAY_ELEM_HEIGHT, HEAD_POS_X, HEAD_POS_Y);

        this.cmd("CreateLabel", tailLabelID, "尾", TAIL_LABEL_X, TAIL_LABEL_Y);
        this.cmd("CreateRectangle", this.tailID, 0, ARRAY_ELEM_WIDTH, ARRAY_ELEM_HEIGHT, TAIL_POS_X, TAIL_POS_Y);



        this.cmd("CreateLabel", this.leftoverLabelID, "", QUEUE_LABEL_X, QUEUE_LABEL_Y);


        this.initialIndex = this.nextIndex;

        this.highlight1ID = this.nextIndex++;
        this.highlight2ID = this.nextIndex++;

        this.animationManager.StartNewAnimation(this.commands);
        this.animationManager.skipForward();
        this.animationManager.clearHistory();


    }
    reset() {
        this.top = 0;
        this.nextIndex = this.initialIndex;

    }
    enqueueCallback(event) {
        if ((this.tail + 1) % SIZE != this.head && this.enqueueField.value != "") {
            var pushVal = this.enqueueField.value;
            this.enqueueField.value = "";
            this.implementAction(this.enqueue.bind(this), pushVal);
        }
    }
    dequeueCallback(event) {
        if (this.tail != this.head) {
            this.implementAction(this.dequeue.bind(this), "");
        }
    }
    clearCallback(event) {
        this.implementAction(this.clearAll.bind(this), "");
    }
    enqueue(elemToEnqueue) {
        this.commands = new Array();

        var labEnqueueID = this.nextIndex++;
        var labEnqueueValID = this.nextIndex++;
        this.arrayData[this.tail] = elemToEnqueue;
        this.cmd("SetText", this.leftoverLabelID, "");

        this.cmd("CreateLabel", labEnqueueID, "入队元素: ", QUEUE_LABEL_X, QUEUE_LABEL_Y);
        this.cmd("CreateLabel", labEnqueueValID, elemToEnqueue, QUEUE_ELEMENT_X, QUEUE_ELEMENT_Y);

        this.cmd("Step");
        this.cmd("CreateHighlightCircle", this.highlight1ID, INDEX_COLOR, TAIL_POS_X, TAIL_POS_Y);
        this.cmd("Step");

        var xpos = (this.tail % ARRRAY_ELEMS_PER_LINE) * ARRAY_ELEM_WIDTH + ARRAY_START_X;
        var ypos = Math.floor(this.tail / ARRRAY_ELEMS_PER_LINE) * ARRAY_LINE_SPACING + ARRAY_START_Y;

        this.cmd("Move", this.highlight1ID, xpos, ypos + ARRAY_ELEM_HEIGHT);
        this.cmd("Step");

        this.cmd("Move", labEnqueueValID, xpos, ypos);
        this.cmd("Step");

        this.cmd("Settext", this.arrayID[this.tail], elemToEnqueue);
        this.cmd("Delete", labEnqueueValID);

        this.cmd("Delete", this.highlight1ID);

        this.cmd("SetHighlight", this.tailID, 1);
        this.cmd("Step");
        this.tail = (this.tail + 1) % SIZE;
        this.cmd("SetText", this.tailID, this.tail);
        this.cmd("Step");
        this.cmd("SetHighlight", this.tailID, 0);
        this.cmd("Delete", labEnqueueID);

        return this.commands;
    }
    dequeue(ignored) {
        this.commands = new Array();

        var labDequeueID = this.nextIndex++;
        var labDequeueValID = this.nextIndex++;

        this.cmd("SetText", this.leftoverLabelID, "");


        this.cmd("CreateLabel", labDequeueID, "出队中: ", QUEUE_LABEL_X, QUEUE_LABEL_Y);

        this.cmd("CreateHighlightCircle", this.highlight1ID, INDEX_COLOR, HEAD_POS_X, HEAD_POS_Y);
        this.cmd("Step");

        var xpos = (this.head % ARRRAY_ELEMS_PER_LINE) * ARRAY_ELEM_WIDTH + ARRAY_START_X;
        var ypos = Math.floor(this.head / ARRRAY_ELEMS_PER_LINE) * ARRAY_LINE_SPACING + ARRAY_START_Y;

        this.cmd("Move", this.highlight1ID, xpos, ypos + ARRAY_ELEM_HEIGHT);
        this.cmd("Step");

        this.cmd("Delete", this.highlight1ID);


        var dequeuedVal = this.arrayData[this.head];
        this.cmd("CreateLabel", labDequeueValID, dequeuedVal, xpos, ypos);
        this.cmd("Settext", this.arrayID[this.head], "");
        this.cmd("Move", labDequeueValID, QUEUE_ELEMENT_X, QUEUE_ELEMENT_Y);
        this.cmd("Step");

        this.cmd("SetHighlight", this.headID, 1);
        this.cmd("Step");
        this.head = (this.head + 1) % SIZE;
        this.cmd("SetText", this.headID, this.head);
        this.cmd("Step");
        this.cmd("SetHighlight", this.headID, 0);

        this.cmd("SetText", this.leftoverLabelID, "出队元素: " + dequeuedVal);


        this.cmd("Delete", labDequeueID);
        this.cmd("Delete", labDequeueValID);



        return this.commands;
    }
    clearAll() {
        this.commands = new Array();
        this.cmd("SetText", this.leftoverLabelID, "");

        for (var i = 0; i < SIZE; i++) {
            this.cmd("SetText", this.arrayID[i], "");
        }
        this.head = 0;
        this.tail = 0;
        this.cmd("SetText", this.headID, "0");
        this.cmd("SetText", this.tailID, "0");
        return this.commands;

    }
}